<%@ page pageEncoding="UTF-8"%>
<%@ include file="/common/token.jsp"%>
<%@ include file="/common/verfy.jsp"%>
<%@ include file="/common/message.jsp"%>
<%
    Integer id = input.getInt("schedule_id", -1);   // 排班id
    Integer pid = input.getInt("pid", -1);          // 就这人id
    String startTime = input.getDesc("start_time", "");   // 开始时间
    String endTime = input.getDesc("end_time", "");     // 结束时间
	String type = input.getDesc("type", "expert");     // 挂号类型
	
	// 查询就诊人
	List<String> names = QBuilder.db("basedb").select("name").from("UserProfile").where(new Where("hospital =", hospital).and("status = ", STATUS_ENABLE).and("id=", pid).and("uid=", token.getUid())).query().getResultObj();
    if (isEmpty(names)) {
        msg = new Message(30041, "用户信息错误");
        Tools.encryptPrint(out, msg);
        return;
    }
	
	if ("1".equals(checkSource(application.getRealPath("/") + appType + "/" + hospital + "/conf.xml")) && "normal".equals(type)) {  // 普通号并实时挂号
		List<Dept> depts = QBuilder.db("basedb").select().from("Dept").where(new Where("hospital =", hospital).and("status = ", STATUS_ENABLE).and("id=", id)).query().getResultObj();
		if (depts.size() == 0) {
			msg = new Message(30041, "号源失效");
			Tools.encryptPrint(out, msg);
			return;
		}
		Dept dept = depts.get(0);
		RegisterOrder registerOrder = new RegisterOrder();
		registerOrder.setHospital(hospital);
		registerOrder.setRegisterStatus(1);
		registerOrder.setUserProfileId(pid);
		registerOrder.setPatientName(names.get(0));
		registerOrder.setDeptId(dept.getDeptId());
		registerOrder.setDeptName(dept.getDeptName());
		registerOrder.setDoctorId("");
		registerOrder.setDoctorName("");
		registerOrder.setDate(DateUtil.formatDate(new Date()));
		registerOrder.setHalf(getHalf());
		registerOrder.setBranch(dept.getBranch());
		registerOrder.setJson("start_time", startTime);
		registerOrder.setJson("end_time", endTime);
		id = (Integer)DBTools.db("basedb").addItem(registerOrder);
		if (isEmpty(id)) {
			msg = new Message(30031, "预约失败");
		} else {
			msg.put("data", id);
			createMsg(registerOrder, token.getUid());
		}
		Tools.encryptPrint(out, msg);
		
	} else {
		List<Schedule> shcedules = QBuilder.db("basedb").select().from("Schedule").where(new Where("hospital =", hospital).and("status = ", STATUS_ENABLE).and("id=", id)).query().getResultObj();
		if (shcedules.size() == 0) {
			msg = new Message(30041, "号源失效");
			Tools.encryptPrint(out, msg);
			return;
		}
		Schedule schedule = shcedules.get(0);
		List<Map> orders = QBuilder.db("basedb").select("new map(id as register_id, patientName as patient_name, doctorName as doctor_name, deptName as dept_name, date as date, half as half, registerStatus as register_status, branch as branch, jsonString as json)").from("RegisterOrder").where(new Where("hospital=", hospital).and("status=", STATUS_ENABLE).and("userProfileId =", pid).and("date=", schedule.getDate())).query().getResultObj();
		if (orders.size() > 0) {
			msg = new Message(30041, "该用户已经预约当天号源,请勿重复预约");
			Tools.encryptPrint(out, msg);
			return;
		}
		RegisterOrder registerOrder = new RegisterOrder();
		registerOrder.setHospital(hospital);
		registerOrder.setRegisterStatus(1);
		registerOrder.setUserProfileId(pid);
		registerOrder.setPatientName(names.get(0));
		registerOrder.setDeptId(schedule.getDeptId());
		registerOrder.setDeptName(schedule.getDeptName());
		registerOrder.setDoctorId(schedule.getDoctorId());
		registerOrder.setDoctorName(schedule.getDoctorName());
		registerOrder.setDate(schedule.getDate());
		registerOrder.setHalf(schedule.getHalf()+"");
		registerOrder.setBranch(schedule.getBranch());
		registerOrder.setJson("start_time", startTime);
		registerOrder.setJson("end_time", endTime);
		//registerOrder.setOrderId();
		id = (Integer)DBTools.db("basedb").addItem(registerOrder);
		if (isEmpty(id)) {
			msg = new Message(30031, "预约失败");
		} else {
			msg.put("data", id);
			createMsg(registerOrder, token.getUid());
		}
		Tools.encryptPrint(out, msg);
	}
    
%>
